﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel;
using EntityDataModel;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using BaseUtilities;

namespace Controller.DataProvider
{
    public class ProductPro : GenericProvider<Product>
    {
        public ProductPro()
        {
            EntityName = "Tbl_Product";
        }

        #region override Methods
        //----------------------------------------
        public override EntityObject CreateEntity()
        {
            return new Tbl_Product();
        }
        //----------------------------------------
        public override Product CreateModel()
        {
            return new Product();
        }
        //----------------------------------------
        public override void ToEntity(Product pModel, EntityObject pEntity, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_Product oEntity = pEntity as Tbl_Product;
            oEntity.TypeId = pModel.TypeId;
            oEntity.UnitId = pModel.UnitId;
            oEntity.Code = pModel.Code;
            oEntity.Name = pModel.Name;
            oEntity.Quantity = pModel.Quantity;
            oEntity.SalePriceUnit = pModel.SalePriceUnit;
            oEntity.Note = pModel.Note;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        oEntity.Id = pModel.Id;
                        break;
                    case EnumFieldCopy.Created:
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        break;
                    case EnumFieldCopy.Modified:
                        oEntity.ModifiedBy = pModel.ModifiedBy;
                        oEntity.ModifiedDate = pModel.ModifiedDate;
                        break;
                    case EnumFieldCopy.Childs:
                        UnitChangePro detailPro = new UnitChangePro();
                        pModel.UnitChanges.ToList().ForEach((child) =>
                        {
                            Tbl_UnitChange detail = oEntity.Tbl_UnitChange.Where(p => p.Id == child.Id).FirstOrDefault();
                            if (detail == null)
                            {
                                detail = new Tbl_UnitChange();
                                oEntity.Tbl_UnitChange.Add(detail);
                            }
                            detailPro.ToEntity(child, detail, EnumFieldCopy.All);
                        });
                        break;
                    case EnumFieldCopy.All:
                        oEntity.Id = pModel.Id;
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        oEntity.ModifiedBy = pModel.ModifiedBy;
                        oEntity.ModifiedDate = pModel.ModifiedDate;
                        break;
                }
            });
        }
        //----------------------------------------
        public override void ToModel(EntityObject pEntity, Product pModel, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_Product oEntity = pEntity as Tbl_Product;
            pModel.TypeId = oEntity.TypeId;
            pModel.UnitId = oEntity.UnitId;
            pModel.Code = oEntity.Code;
            pModel.Name = oEntity.Name;
            pModel.Quantity = oEntity.Quantity;
            pModel.SalePriceUnit = oEntity.SalePriceUnit;
            pModel.Note = oEntity.Note;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        pModel.Id = oEntity.Id;
                        break;
                    case EnumFieldCopy.Created:
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        break;
                    case EnumFieldCopy.Modified:
                        pModel.ModifiedBy = oEntity.ModifiedBy;
                        pModel.ModifiedDate = oEntity.ModifiedDate;
                        break;
                    case EnumFieldCopy.Relation:
                        if (oEntity.Tbl_ProductType != null)
                        {
                            pModel.TypeName = oEntity.Tbl_ProductType.Name;
                            pModel.TypeCode = oEntity.Tbl_ProductType.Code;
                        }
                        if (oEntity.Tbl_Unit != null)
                            pModel.UnitName = oEntity.Tbl_Unit.Name;
                        break;
                    case EnumFieldCopy.Childs:
                        UnitChangePro detailPro = new UnitChangePro();
                        oEntity.Tbl_UnitChange.ToList().ForEach((child) =>
                        {
                            UnitChange detail = pModel.UnitChanges.Where(p => p.Id == child.Id).FirstOrDefault();
                            if (detail == null)
                            {
                                detail = new UnitChange();
                                pModel.UnitChanges.Add(detail);
                            }
                            detailPro.ToModel(child, detail, EnumFieldCopy.All);
                        });
                        break;
                    case EnumFieldCopy.All:
                        pModel.Id = oEntity.Id;
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        pModel.ModifiedBy = oEntity.ModifiedBy;
                        pModel.ModifiedDate = oEntity.ModifiedDate;
                        if (oEntity.Tbl_ProductType != null)
                        {
                            pModel.TypeName = oEntity.Tbl_ProductType.Name;
                            pModel.TypeCode = oEntity.Tbl_ProductType.Code;
                        }
                        if (oEntity.Tbl_Unit != null)
                            pModel.UnitName = oEntity.Tbl_Unit.Name;
                        break;
                }
            });
        }
        #endregion override Methods

        #region Public Methods
        //----------------------------------------
        public ResultModel GetAllByTypeId(int pTypeId)
        {
            IList<Product> result = new List<Product>();
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                List<Tbl_Product> lst = entities.Tbl_Product.Where(p => p.TypeId == pTypeId).ToList();
                lst.ForEach((item) =>
                {
                    Product oProduct = new Product();
                    ToModel(item, oProduct, EnumFieldCopy.All);
                    result.Add(oProduct);
                });
            }
            return new ResultModel(result);
        }
        //----------------------------------------
        public ResultModel GetIdNameAllByTypeId(int pTypeId)
        {
            IList<IdNameModel> result = null;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                result = (from p in entities.Tbl_Product
                          where p.TypeId == pTypeId
                          select new
                          {
                              p.Id,
                              p.Name
                          }).AsEnumerable().Select(p => new IdNameModel { Id = p.Id, Name = p.Name }).ToList();
            }
            return new ResultModel(result);
        }
        //----------------------------------------
        public ResultModel GetTypeById(int pId)
        {
            IdNameModel result = null;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                int iTypeId = entities.Tbl_Product.Where(p => p.Id == pId).Select(p => p.TypeId).FirstOrDefault();
                if (iTypeId > 0)
                {
                    result = (from p in entities.Tbl_ProductType
                              where p.Id == iTypeId
                              select new
                              {
                                  p.Id,
                                  p.Name
                              }).AsEnumerable().Select(p => new IdNameModel { Id = p.Id, Name = p.Name }).FirstOrDefault();
                }
            }
            return new ResultModel(result);
        }
        //----------------------------------------
        public ResultModel GetAllCode()
        {
            IList<IdNameModel> result = null;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                result = (from p in entities.Tbl_Product
                          select new
                          {
                              p.Id,
                              p.Code
                          }).AsEnumerable().Select(p => new IdNameModel { Id = p.Id, Name = p.Code }).ToList();
            }
            return new ResultModel(result);
        }
        //----------------------------------------
        public ResultModel GetAllName()
        {
            IList<IdNameModel> result = null;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                result = (from p in entities.Tbl_Product
                          select new
                          {
                              p.Id,
                              p.Name
                          }).AsEnumerable().Select(p => new IdNameModel { Id = p.Id, Name = p.Name }).ToList();
            }
            return new ResultModel(result);
        }
        //----------------------------------------
        public ResultModel GetAllCbb()
        {
            IList<Product> result = null;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                result = (from p in entities.Tbl_Product
                          select new Product
                          {
                              Id = p.Id,
                              TypeId = p.TypeId,
                              UnitId = p.UnitId,
                              Code = p.Code,
                              Name = p.Name,
                              Quantity = p.Quantity
                          }).ToList();
            }
            return new ResultModel(result);
        }
        #endregion Public Methods
    }
}
